package ynu;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Movie {
    private String name; // 片名
    private String director; // 导演
    private String actors; // 主演
    private String description; // 剧情简介
    private int duration; // 时长(分钟)
    private double rating; // 评分(0-10分)

    public Movie(String name, String director, String actors, String description,
                 int duration, double rating) {
        this.name = name;
        this.director = director;
        this.actors = actors;
        this.description = description;
        this.duration = duration;
        this.rating = rating;
    }

    // 读取所有影片信息
    public static List<Movie> readMovies() {
        List<Movie> movies = new ArrayList<>();
        File file = new File("D:\\java实训大作业\\影片信息.txt");

        if (!file.exists()) {
            return movies;
        }

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\|");
                if (parts.length == 6) {
                    movies.add(new Movie(parts[0], parts[1], parts[2], parts[3],
                            Integer.parseInt(parts[4]),
                            Double.parseDouble(parts[5])));
                }
            }
        } catch (IOException e) {
            System.out.println("读取影片信息出错: " + e.getMessage());
        }
        return movies;
    }

    // 保存所有影片信息
    public static void saveMovies(List<Movie> movies) {
        File dir = new File("D:\\java实训大作业");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                "D:\\java实训大作业\\影片信息.txt"))) {
            for (Movie movie : movies) {
                bw.write(movie.name + "|" + movie.director + "|" + movie.actors + "|" +
                        movie.description + "|" + movie.duration + "|" + movie.rating);
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("保存影片信息出错: " + e.getMessage());
        }
    }

    // 查找影片
    public static Movie findMovie(List<Movie> movies, String name) {
        for (Movie movie : movies) {
            if (movie.name.equals(name)) {
                return movie;
            }
        }
        return null;
    }

    // getter和setter方法
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getDirector() { return director; }
    public void setDirector(String director) { this.director = director; }
    public String getActors() { return actors; }
    public void setActors(String actors) { this.actors = actors; }
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public int getDuration() { return duration; }
    public void setduration(int duration) { this.duration = duration; }
    public double getRating() { return rating; }
    public void setRating(double rating) { this.rating = rating; }
}
